home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2055 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  40 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.Stanford.EDU!microunity!toms
  3. From: toms@MicroUnity.com (Tom Sanders)
  4. Subject: Re: Help, best way to compare doubles
  5. Message-ID: <DLE3uF.yL@microunity.com>
  6. Sender: usenet@microunity.com (news id)
  7. Organization: MicroUnity Systems Engineering, Inc.
  8. References: <4da169$mm4@mercury.IntNet.net> <4dfdtl$phv@nntp.crl.com> <DLD5Kp.JME@eskimo.com>
  9. Date: Thu, 18 Jan 1996 18:39:50 GMT
  10.  
  11. In article <DLD5Kp.JME@eskimo.com>, mag@eskimo.com (mAg) writes:
  12. |> In article <4dfdtl$phv@nntp.crl.com> (16 Jan 1996 05:43:17 GMT), 
  13. |> bivey@ecf2.puc.edu says :
  14. |> >
  15. |> >Jeff Tomich (jtomich@IntNet.net) wrote:
  16. |> >: Looking for a way to compare doubles for equality.
  17. |> >
  18. |> >: thanks, Jeff
  19. |> >
  20. |> >The first question to ask is "how equal"?  In general, one picks some
  21. |> >error limit, such as  e = 1E-6;   then uses it in this form:
  22. |> >
  23. |> >   if (fabs(n1 - n2) < e)  // then they are considered equal
  24. |> >   else                    // they are considered different
  25. |> >
  26. |> 
  27. |> "How equal" is irrelevant. "Equal to" comparison produces a boolean result 
  28. |> True or False. 
  29. |> 
  30. |> '==' is good enough
  31. |> 
  32.  
  33. If any numeric computations have been performed on the 2 doubles '==' is 
  34. inappropriate due to numeric computation errors.  I have always preferred
  35. first rounding both operands to the same number of digits prior to a
  36. comparison.  How many digits is very application dependent.
  37.  
  38. Tom Sanders
  39.  
  40.